import pyhooked
import win32clipboard as w
import win32con


_is_copy = False


def getText():
    w.OpenClipboard()
    d = w.GetClipboardData(win32con.CF_UNICODETEXT)
    w.CloseClipboard()
    return d

def setText(aString):
    w.OpenClipboard()
    w.EmptyClipboard()
    w.SetClipboardData(win32con.CF_UNICODETEXT, aString)
    w.CloseClipboard()

def handler(e):
    global _is_copy
    if e.event_type == 'key down' and 'C' in e.pressed_key and ('Lcontrol' in e.pressed_key or 'Rcontrol' in e.pressed_key):
        _is_copy = True
    if _is_copy and e.event_type == 'key up' and not e.pressed_key:
        _is_copy = False
        t = getText().replace('\r', '').replace('-\n', '').replace('\n', ' ').strip()
        setText(t)
        print(t)
        print()
        

def listen_keyboard():
    hook = pyhooked.Hook()
    hook.handler = handler
    hook.hook()
    

if __name__ == '__main__':
    listen_keyboard()
